<?php
class CoreLib extends Core
{
	public static function parseType($var)
	{
		$conv = null;

		switch(gettype($var)){
			case "string":{
				if ( substr($var, 0, 1) == "{" ) {// Objeto JSON
					$conv = $var;
					break;
				}

				$conv = preg_replace("/[\"\']/", "\'", $var);
				$conv = "'$conv'";

				break;
			}
			case ("integer" || "double"):{
				$conv = "$var";

				break;
			}
		}

		return($conv);
	}

	public static function getTime()
	{
		list($foo, $sec) = explode(" ", microtime());
		return ((float)$sec);
	}

	public static function getFilename($url)
	{
		$pos = strrpos($url, "/");

		if ( !$pos )
			return($url);

		$url = substr($url, $pos + 1);
		$pos = strrpos($url, ".");

		if ( !$pos )
			return($url);

		$url = substr($url, 0, $pos);

		return($url);
	}

	public static function addCompat($libname)
	{
		if ( !ClassLoader::getInstance()->loadClass( $libname ) ) {
			return(false);
		}

		return(true);
	}

	public static function json_decode($data, $assoc = false)
	{
		if ( !self::addCompat("JSON") )
			die("CORE_FAULT");

		//$json = new Services_JSON();
		$obj = json_decode($data);

		if ( $assoc && is_object($obj) )
		{
			$objArr = array();

			foreach( $obj as $key => $value ) {
				$objArr[$key] = $value;
			}

			$obj = $objArr;
		}

		return( $obj );
	}

	public static function json_encode($data)
	{
		if ( !self::addCompat("JSON") )
			die("CORE_FAULT");

		//$json = new Services_JSON();

		return( json_encode($data) );
	}
}
?>
